#include <cstdio>
#include <cstring>
#include <queue>

const int N = 505, inf = 0x3f3f3f3f;
int n, m, dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}, dis[N][N],
          wal[4][N][N];  // [0] L, [1] R, [2] T, [3] B
char map[N][N];
bool vis[N][N];

struct Node {
  int x, y;
  Node(int _x, int _y) : x(_x), y(_y) {}
} C(0, 0), F(0, 0);

std::queue<Node> q;

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("portal.in", "r", stdin);
  freopen("portal.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    wal[1][i][m + 1] = m + 1;
  }
  for (int i = 1; i <= m; ++i) {
    wal[3][n + 1][i] = n + 1;
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%s", map[i] + 1);
    for (int j = 1; j <= m; ++j) {
      if (map[i][j] == 'C') {
        C = Node(i, j);
      } else if (map[i][j] == 'F') {
        F = Node(i, j);
      }
      if (map[i][j] == '#') {
        wal[0][i][j] = wal[1][i][j] = j;
        wal[2][i][j] = i, wal[3][i][j] = i;
      } else {
        wal[0][i][j] = wal[0][i][j - 1];
        if (map[i - 1][j] == '#')
          wal[2][i][j] = i - 1;
        else
          wal[2][i][j] = wal[2][i - 1][j];
      }
    }
    for (int j = m; j; --j) {
      if (map[i][j] != '#') {
        wal[1][i][j] = wal[1][i][j + 1];
      }
    }
  }
  for (int i = n; i; --i) {
    for (int j = 1; j <= m; ++j) {
      if (map[i][j] != '#') {
        wal[3][i][j] = wal[3][i + 1][j];
      }
    }
  }
  memset(dis, inf, sizeof(dis));
  dis[C.x][C.y] = 0;
  q.push(C);
  while (!q.empty()) {
    Node u = q.front();
    q.pop();
    for (int k = 0, tx, ty, i, j; k < 4; ++k) {
      i = u.x, j = u.y;
      tx = i + dx[k], ty = j + dy[k];
      if (tx > 0 && tx <= n && ty > 0 && ty <= m) {
        if (map[tx][ty] == 'F') {
          if (dis[tx][ty] > dis[i][j] + 1) {
            dis[tx][ty] = dis[i][j] + 1;
          }
        }
        if (map[tx][ty] == '.') {
          if (dis[tx][ty] > dis[i][j] + 1) {
            dis[tx][ty] = dis[i][j] + 1;
            if (!vis[tx][ty]) q.push(Node(tx, ty));
          }
        }
        if (map[tx][ty] == '#') {
          tx = wal[2][i][j] + 1;
          if (tx != i + dx[k] + 1)
            if (dis[tx][j] > dis[i][j] + 1) {
              dis[tx][j] = dis[i][j] + 1;
              if (!vis[tx][j]) q.push(Node(tx, j));
            }
          tx = wal[3][i][j] - 1;
          if (tx != i + dx[k] - 1)
            if (dis[tx][j] > dis[i][j] + 1) {
              dis[tx][j] = dis[i][j] + 1;
              if (!vis[tx][j]) q.push(Node(tx, j));
            }
          ty = wal[0][i][j] + 1;
          if (ty != j + dy[k] + 1)
            if (dis[i][ty] > dis[i][j] + 1) {
              dis[i][ty] = dis[i][j] + 1;
              if (!vis[i][ty]) q.push(Node(i, ty));
            }
          ty = wal[1][i][j] - 1;
          if (ty != j + dy[k] - 1)
            if (dis[i][ty] > dis[i][j] + 1) {
              dis[i][ty] = dis[i][j] + 1;
              if (!vis[i][ty]) q.push(Node(i, ty));
            }
        }
      }
    }
  }
  if (dis[F.x][F.y] == inf)
    puts("nemoguce");
  else
    printf("%d", dis[F.x][F.y]);
  return 0;
}